home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / GoodBye / JohnKennedy / February96.lha / MASTERCLASS / MASTERAUG.DOC < prev    next >
Encoding:
Text File  |  1996-06-02  |  7.8 KB  |  268 lines

  1. MasterClass August
  2.  
  3.  
  4. If our last scheduled look at ARexx for a while (stop
  5. cheering at the back of the class!) it's probably a good
  6. idea for us to examine the various ways of testing and
  7. debugging programs and scripts.
  8.  
  9. Even the best programmers in the world write programs with
  10. bugs in them: even me. The knack is to reduce the number and
  11. severity of the bugs to acceptable levels. It's extremely
  12. dangerous to pronouce a program is 100% bug free.
  13.  
  14. ARexx is a forgiving and flexible language, and will make a
  15. good stab at pointing out any of your most common mistakes.
  16. ARexx also includes some excellent debugging tools for
  17. really getting to grips with your programs and killing the
  18. bugs. (Hey! Did this language really come free with my
  19. Amiga?)
  20.  
  21.  
  22. Common Errors
  23.  
  24. Prevention is always better than cure, and so it's
  25. worthwhile looking at the most common mistakes upfront
  26. rather than trying to debug them after they have been typed
  27. in. Common mistakes are:
  28.  
  29. * Forgetting to start RexxMast. If RexxMast isn't present,
  30. then ARexx scripts cannot be run. Make sure you have copied
  31. it from your Workbench Extras disk.
  32.  
  33. * Trying to run a script with the wrong name. You can run an
  34. ARexx script from the Shell with the "RX" command. RX must
  35. be followed by the full name of the program, although if the
  36. program name ends in ".REXX" you can leave this part out.
  37.  
  38. * If no matter what you type to start the ARexx program, it
  39. keeps returning "Program Not Found" it could be because you
  40. have left out the comment at the start of the program. All
  41. ARexx programs must begin with /* and */ at the very start.
  42.  
  43. * Sometimes ARexx seems to totally ignore commands in the
  44. script. For example, although Arexx uses the command "SAY"
  45. to display text you might accidentally use "PRINT" instead.
  46. ARexx will simply ignore this. However, if you use "ECHO" it
  47. will work. What's going on? If you use PRINT ARexx thinks
  48. you are using a command from another Host. Unless you happen
  49. to have a program called "print.rexx" present, nothing will
  50. happen. However, when you use Echo text is displayed,
  51. because by default ARexx will let AmigaDOS have a bash and
  52. recognising commands, and of course Echo is a valid AmigaDOS
  53. command.
  54.  
  55. * All the commands following an IF/THEN clause are not
  56. executed. Let's say you are testing a variable and want
  57. ARexx to perform some actions, like this:
  58.  
  59. IF score>highscore THEN
  60.     SAY "New high score!"
  61.     highscore=score
  62.  
  63. Can you spot the bug? After a THEN, only one statement will
  64. be executed which means the statement "highscore=score" will
  65. always be executed. The correct form is:
  66.  
  67. IF score>highscore THEN
  68.     DO
  69.         SAY "New high score!"
  70.         highscore=score
  71.     END
  72.  
  73. as this packets up the statements into one DO clause.
  74.  
  75.  
  76. * Keep getting "Arithmetic Conversion errors"? This program
  77. may look OK at first glance, but it will crash out with the
  78. arithmetic error. (error.iff)
  79.  
  80. /* Whoops!*/
  81.  
  82. x=y+10
  83. say x
  84.  
  85.  
  86. The problem is that by default all variables are defined to
  87. containt their own name. This means that the variable Y does
  88. not contain a number (you might expect the value "0") and
  89. instead contains the string "Y". When you try and add a
  90. string to a number you get an error. Do it this way instead:
  91.  
  92. /* Better! */
  93.  
  94. y=0
  95. x=y+10
  96. say x
  97.  
  98.  
  99. * Other sorts of errors can be harder to detect. For
  100. example, your script may contain a "logic error" in that
  101. your code is perfect, but what your reasoning is flawed. The
  102. program is not bugged as such, you are! ARexx still makes it
  103. easier to find these flaws too, as we'll see.
  104.  
  105.  
  106.  
  107.  
  108. Stamping on bugs!
  109.  
  110. A good way to find out what your program is currently up to
  111. is to include "SAY" commands at various points in the
  112. program. For example, if your program is quite large and
  113. uses a lot of functions, it might be a good idea to include
  114. SAY at the begin and end of each function. You can use the
  115. SAY simply to indicate which of the program is actually
  116. executing, or you can use SAY to display the current value
  117. of a variable.
  118.  
  119. It can also be a good idea to make the SAY conditional, so
  120. that you can switch the message on and off:
  121.  
  122. if debug=1 then say "Function Zog Started"
  123.  
  124. You can control the printing of messages by setting "debug"
  125. to 1 or 0 at the start of the script.
  126.  
  127. However, this is nothing new and in fact ARexx can do it
  128. pretty much automatically, thanks to the TRACE command. At
  129. any point in your program you can include the line TRACE ALL
  130. to switch on special trace output, and then TRACE NORMAL to
  131. switch it back on. Like this:
  132.  
  133.  
  134. /* Tracing Example */
  135.  
  136. say "This is an example.."
  137. do a=1 to 5
  138.     b=a*10
  139. end
  140.  
  141. trace all
  142. do a=6 to 10
  143.     b=a*10
  144. end
  145. trace normal
  146.  
  147.  
  148. This creates output like this:
  149.  
  150. This is an example..
  151.   9 *-* do a=6 to 10;
  152.    10 *-* b=a*10;
  153.    11 *-* end;
  154.     9 *-* do a=6 to 10;
  155.    10 *-* b=a*10;
  156.    11 *-* end;
  157.     9 *-* do a=6 to 10;
  158.    10 *-* b=a*10;
  159.    11 *-* end;
  160.     9 *-* do a=6 to 10;
  161.    10 *-* b=a*10;
  162.    11 *-* end;
  163.     9 *-* do a=6 to 10;
  164.    10 *-* b=a*10;
  165.    11 *-* end;
  166.     9 *-* do a=6 to 10;
  167.  12 *-* trace normal;
  168.  
  169.  
  170. (trace.iff -- Let ARexx show you what's happening with the
  171. Trace command)
  172.  
  173. Up until the Trace command, nothing happens. However, after
  174. it the display contains the line number of the script,
  175. followed by the statement currently being executed. This
  176. allows you to see which lines in your program are being
  177. processed. Remember, if the text whizzes off your screen too
  178. quickly to see you can redirect it. For example, if the
  179. program is called "plop.rexx", run it like this:
  180.  
  181. rx plop > ram:debug
  182.  
  183. This will sent the output of the program to a file called
  184. "debug" in the Ram disk where you can peruse it at your
  185. leisure.
  186.  
  187. Indidentally, you can use TRACE with several options instead
  188. of FULL such as "RESULTS", which will also display the value
  189. of any expressions calcutated. Use "INTERMEDIATE" if you
  190. want a lot of extra information too.
  191.  
  192. Sometimes these debugging methods can be confusing,
  193. especially if your program already prints to the screen. For
  194. this reason, ARexx can open a new window especially for
  195. trace output. All you have to do is enter
  196.  
  197. TCO
  198.  
  199. at the shell. This stands for "Trace Console Open" and it
  200. will open a new window. This window will display all the
  201. output generate by TRACE. You can close the window by
  202. entering 
  203.  
  204. TCC
  205.  
  206. which stands for "Trace Console Close".
  207.  
  208.  
  209. tco.iff
  210. If asked nicely, ARexx will open up a new window for the
  211. display of all trace output.
  212.  
  213.  
  214.  
  215. If you want to be able to control how the trace operates
  216. whilst the program is running, use another TRACE option such
  217. as this:
  218.  
  219. trace ?all
  220.  
  221. which puts the Trace into a special interactive mode. Now
  222. the program will halt after every command with a prompt. You
  223. can either continue to the next instruction by pressing
  224. return, or enter legitimake ARexx commands. For example,
  225. you could end SAY to check on the contents of a variable. Or
  226. switch off the interactive mode by typing:
  227.  
  228. trace ?
  229.  
  230. at the prompt again. You can also enter a number which
  231. causes that many commands to be ignored. This is especially
  232. useful when dealing with large loops.
  233.  
  234.  
  235. The end
  236.  
  237. And that's about it! If there are any specific ARexx
  238. questions you which to ask, or if you have any suggestions
  239. for future MasterClass topics, please get in touch either
  240. via the CU address of via email. The email address is
  241. johnk@infosys2.thegap.com, and my Web site is
  242. http://www.webzone1.co.uk/www/johnk
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249. Box out: Good news for ARexx fans!
  250.  
  251. Last month there was excellent news for graphics users with
  252. a penchant for ARexx, as it was announced that new versions
  253. of both the Photogenics art package and the Imagine
  254. rendering software are to include ARexx support.
  255.  
  256. For Photogenics this means the ability "batch process" large
  257. numbers of files, as well as define custom built commands.
  258. Imagine users will have the ability to create new object
  259. tools and have an extraordinary degree of control over
  260. animations. With some ARexx scripts it would be possible to
  261. control the position and shape of Imagine objects
  262. mathematically -- let me at it!
  263.  
  264.  
  265.  
  266.  
  267.  
  268.